package com.whu.controller;

import com.whu.entity.Product;
import com.whu.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping
    List<Product> getAll(){
        return productService.getAll();
    }

    @GetMapping("/{id}")
    Product getById(@PathVariable Long id){
        return productService.getById(id);
    }

    @PostMapping
    boolean save(@RequestBody Product product){
        return productService.saveWithSuppliers(product);
    }

    @DeleteMapping("/{id}")
    boolean delete(@PathVariable Long id){
        return productService.deleteByIdWithSuppliers(id);
    }

    @PutMapping
    boolean update(@RequestBody Product product){
        return productService.updateById(product);
    }

}
